package com.zc.emos.wx.controller;

import cn.hutool.json.JSONUtil;
import com.zc.emos.wx.common.util.R;
import com.zc.emos.wx.config.shiro.JwtUtil;
import com.zc.emos.wx.config.tencent.TLSSigAPIv2;
import com.zc.emos.wx.controller.form.*;
import com.zc.emos.wx.exception.EmosException;
import com.zc.emos.wx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description 用户注册接口
 * @createTime 2021年12月22日 15:14:00
 */
@RestController
@RequestMapping("/user")
@Api("用户模块接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${emos.jwt.cache-expire}")
    private int cacheExpire;

    @Value("${trtc.appid}")
    private Integer appid;

    @Value("${trtc.key}")
    private String key;

    @Value("${trtc.expire}")
    private Integer expire;

    @PostMapping("/register")
    @ApiOperation("注册用户")
    public R register(@Valid @RequestBody RegisterForm form){
        int id = userService.registerUser(form.getRegisterCode(), form.getCode(),
                form.getNickname(), form.getPhoto());
        System.out.println(id);
        String token = jwtUtil.createToken(id);
        Set<String> permsSet = userService.searchUserPermissions(id);
        saveCacheToken(token, id);
        return R.ok("用户注册成功").put("token", token).put("permission", permsSet);
    }


    @PostMapping("/login")
    @ApiOperation("登录接口")
    public R login(@Valid @RequestBody LoginForm form) {
        Integer id = userService.login(form.getCode());
        String token = jwtUtil.createToken(id);
        log.info("======当前 token 为：" + token);
        Set<String> permsSet = userService.searchUserPermissions(id);
        saveCacheToken(token, id);
        return R.ok("用户登录成功").put("token", token).put("permission", permsSet);

    }


    @GetMapping("/searchUserSummary")
    @ApiOperation("查询用户摘要信息")
    public R searchUserSummary(@RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        HashMap map = userService.searchUserSummary(userId);

        return R.ok().put("result", map);

    }

    private void saveCacheToken(String token, int userId){
        redisTemplate.opsForValue().set(token, userId+"",cacheExpire, TimeUnit.DAYS);
    }


    @PostMapping("/searchUserGroupByDept")
    @ApiOperation("查询员工列表,按照部门分组排列")
    @RequiresPermissions(value = {"ROOT", "EMPLOYEE:SELECT"}, logical = Logical.OR)
    public R SearchMyMeetingListByPage(@Valid @RequestBody SearchUserGroupByDeptForm form) {

        ArrayList<HashMap> list = userService.searchUserGroupByDept(form.getKeyword());
        return R.ok().put("result", list);

    }

    @PostMapping("/searchMembers")
    @ApiOperation("查询成员")
    @RequiresPermissions(value = {"ROOT", "MEETING:INSERT", "MEETING:UPDATE",}, logical = Logical.OR)
    public R SearchMembersForm(@Valid @RequestBody SearchMembersForm form) {

        if (!JSONUtil.isJsonArray(form.getMembers())) {
            throw new EmosException("不是json数组");
        }

        List<Integer> param = JSONUtil.parseArray(form.getMembers()).toList(Integer.class);
        ArrayList<HashMap> list = userService.searchMembers(param);
        return R.ok().put("result", list);

    }

    @PostMapping("/selectUserPhotoAndName")
    @ApiOperation("查询用户姓名和头像")
    @RequiresPermissions(value = {"WORKFLOW:APPROVAL"})
    public R selectUserPhotoAndName(@Valid @RequestBody SelectUserPhotoAndNameForm form){
        if(!JSONUtil.isJsonArray(form.getIds())){
            throw new EmosException("参数不是JSON数组");
        }
        List<Integer> param=JSONUtil.parseArray(form.getIds()).toList(Integer.class);
        List<HashMap> list=userService.selectUserPhotoAndName(param);
        return R.ok().put("result",list);
    }

    @GetMapping("/genUserSig")
    @ApiOperation("生成用户签名")
    public R genUserSig(@RequestHeader("token") String token) {
        int id = jwtUtil.getUserId(token);
        TLSSigAPIv2 api = new TLSSigAPIv2(appid, key);
        String userSig = api.genUserSig(id + "", expire);
        return R.ok().put("userSig", userSig).put("id", id);
    }


}
